package com.cy.store.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 创建登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object result = request.getSession().getAttribute("uid");
        System.out.println(result);
        //当session中取不到用户id等信息时，则为没有登录状态，重定向至登录页面
        if(result==null){
            response.sendRedirect("/web/login.html");
            return false;
        }
        return true;
    }
}
